//
//  SideViewController.m
//  iMoc
//
//  Created by 微我网络 on 15/1/10.
//  Copyright (c) 2015年 lamcelot. All rights reserved.
//

#import "SideViewController.h"
#import "SRMe+Cate.h"
#import <UIImageView+AFNetworking.h>
#import "SRBatteryService.h"
#import "SRDevice+Cate.h"

@interface SideViewController ()
{
    UILabel *dclabel;
}

@property (nonatomic,strong) CALayer *batteryLeft;

@end

@implementation SideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"侧边栏"];
    imageView.contentMode = UIViewContentModeScaleToFill;
//    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];
    [self initView];
}

- (void)initView
{
    _sportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sportBtn.frame = CGRectMake(0, 20, 100, 120);
//    _sportBtn.backgroundColor = [UIColor redColor];
    [self.view addSubview:_sportBtn];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_sportBtn.frame), 100, 0.5)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    
    _infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _infoBtn.frame = CGRectMake(0, CGRectGetMaxY(_sportBtn.frame), 100,120);
//    _infoBtn.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_infoBtn];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_infoBtn.frame), 100, 0.5)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    
    _setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _setBtn.frame = CGRectMake(0, CGRectGetMaxY(_infoBtn.frame), 100, 120);
    [self.view addSubview:_setBtn];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_setBtn.frame), 100, 0.5)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    
    UIImageView *sportImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    sportImage.backgroundColor = [UIColor whiteColor];
    sportImage.layer.cornerRadius = 30;
    sportImage.layer.masksToBounds = YES;
    SRMe *me = [SRMe currentUser];
    [sportImage setImageWithURL:[NSURL URLWithString:me.headPhoto] placeholderImage:[UIImage imageNamed:@"头像"]];
    sportImage.center = CGPointMake(_sportBtn.center.x, _sportBtn.center.y - 10);
    [self.view addSubview:sportImage];
    
    UILabel *sportlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(sportImage.frame) + 10, 100, 20)];
    sportlabel.text = me.nickName;
    sportlabel.textColor = [UIColor whiteColor];
    sportlabel.textAlignment = NSTextAlignmentCenter;
    sportlabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:sportlabel];
    
    UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    infoImage.image = [UIImage imageNamed:@"person"];
    [infoImage sizeToFit];
    infoImage.layer.cornerRadius = 30;
    infoImage.center = CGPointMake(_infoBtn.center.x, _infoBtn.center.y - 10);
    [self.view addSubview:infoImage];
    
    UILabel *infolabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(infoImage.frame) + 10, 100, 20)];
    infolabel.text = NSLocalizedString(@"个人信息", nil);
    infolabel.textColor = [UIColor whiteColor];
    infolabel.textAlignment = NSTextAlignmentCenter;
    infolabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:infolabel];
    
    UIImageView *setImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    setImage.image = [UIImage imageNamed:@"configBg"];
    setImage.layer.cornerRadius = 30;
    [setImage sizeToFit];
    setImage.center = CGPointMake(_setBtn.center.x, _setBtn.center.y - 10);
    [self.view addSubview:setImage];
    
    UILabel *setlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(setImage.frame) + 10, 100, 20)];
    setlabel.text = NSLocalizedString(@"设置", nil);
    setlabel.textColor = [UIColor whiteColor];
    setlabel.textAlignment = NSTextAlignmentCenter;
    setlabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:setlabel];
    
    UIImageView *dcImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    dcImage.image = [UIImage imageNamed:@"sideBattery"];
    [dcImage sizeToFit];
    dcImage.center = CGPointMake(_infoBtn.center.x, CGRectGetMaxY(_setBtn.frame) + 40);
    [self.view addSubview:dcImage];
    
    _batteryLeft = [CALayer layer];
    _batteryLeft.backgroundColor = [UIColor whiteColor].CGColor;
    _batteryLeft.cornerRadius = 2;
    _batteryLeft.masksToBounds = YES;
//    _batteryLeft.bounds = CGRectMake(0, 0, 11, 30);
//    _batteryLeft.position = CGPointMake(_infoBtn.center.x, CGRectGetMaxY(_setBtn.frame) + 41.5);
    _batteryLeft.frame = CGRectMake(_infoBtn.center.x-5.5, CGRectGetMaxY(_setBtn.frame) +26.5, 11, 30);
    _batteryLeft.anchorPoint = CGPointMake(0.5, 0);
    [self.view.layer addSublayer:_batteryLeft];
    
    dclabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(dcImage.frame) +10, 100, 20)];
    dclabel.textColor = [UIColor whiteColor];
    dclabel.textAlignment = NSTextAlignmentCenter;
    dclabel.font = [UIFont boldSystemFontOfSize:15];
    [self.view addSubview:dclabel];
    
    [[SRBatteryService readBatteryFromDevice] subscribeNext:^(id x) {
        SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
        dclabel.text = [NSString stringWithFormat:@"%d%%",device.battery.intValue];
        _batteryLeft.frame = CGRectMake(_infoBtn.center.x-5.5,CGRectGetMaxY(_setBtn.frame) +26.5+0.3*(100-device.battery.floatValue),
                                        11, 0.3*device.battery.floatValue);
    } error:^(NSError *error) {
        
    }];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [[SRBatteryService readBatteryFromDevice] subscribeNext:^(id x) {
        SRDevice * device = [SRDevice findDeviceForMe:[SRMe currentUser]];
        dclabel.text = [NSString stringWithFormat:@"%d%%",device.battery.intValue];
        _batteryLeft.frame = CGRectMake(_infoBtn.center.x-5.5,CGRectGetMaxY(_setBtn.frame) +26.5+0.3*(100-device.battery.floatValue),
                                        11, 0.3*device.battery.floatValue);
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
